(function () {
    'use strict';

    angular
        .module('app.login')
        .controller('LoginController', LoginController);

    LoginController.$inject = ['logger','$state','$rootScope','UserContextData','$mdDialog'];
    /* @ngInject */
    function LoginController(logger, $state, $rootScope, UserContextData,$mdDialog) {
        var vm = this;
        vm.title = 'Login';
        vm.user = "";
        vm.password = "";
        vm.ErrorMessage = "";
        vm.LoginClick = LoginClick;
        vm.FPClick = FPClick;
        vm.NewUserClick = NewUserClick;

        activate();
        
        function activate() {


            if($rootScope.isAuthenticated)
            {
                //$state.go('dashboard');
            }
        }

        function LoginClick() {
            showWait();
            try{
            var a = UserContextData.AuthenticateUser(vm.user,vm.password).then(function (response) {
                    $mdDialog.hide();
                vm.ErrorMessage = response.data.ErrorMessage;
                if (response.data.IsAuthenticated) {
                    $rootScope.isAuthenticated = true;
                    $rootScope.UserFullName = response.data.UserLastName + ', ' + response.data.UserFirstName;
                    $rootScope.CurrentLoginID = response.data.LoginID;
                    $rootScope.DataLoaded = false;
                    $rootScope.IsController = response.data.IsController;
                    $rootScope.CurrentPhysicianID = response.data.PhysicianID;
                    $rootScope.CurrentUserName = vm.user;
                    $state.go('dashboard');
                }
            });
            }
            catch(ex)
            {
           
            }
            finally {
            
            }
        };

        function FPClick() {
            $mdDialog.show({
                controller: 'ForgotPasswordController',
                templateUrl: 'app/ForgotPassword/ForgotPassword.html',
                parent: angular.element(document.body),
                controllerAs: 'vm',
                clickOutsideToClose: false,
                fullscreen: false
            }).then(function (answer) {
            }, function () {

            });
        };
        
            
        function NewUserClick() {
            $state.go('NewUser');
        }

        function showWait() {
            $mdDialog.show({
                controller: 'LoginController',
                template: '<md-dialog style="background-color:transparent;box-shadow:none">' +
                            '<div layout="row" layout-sm="column" layout-align="center center" aria-label="wait">' +
                                '<md-progress-circular md-mode="indeterminate" class="md-accent"></md-progress-circular>' +
                            '</div>' +
                         '</md-dialog>',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                fullscreen: false
            })
            .then(function (answer) {

            });
        };
    }
})();


